package fr.generali.accueilclient.gwt.client.ui.components.menu.contextual;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasEnabled;
import com.google.gwt.user.client.ui.Widget;

import fr.generali.accueilclient.gwt.client.privilege.HasGrantedResource;
import fr.generali.accueilclient.gwt.client.privilege.ResourceAccessHolder;
import fr.generali.accueilclient.gwt.client.ui.components.menu.Resources;

public class ContextualMenuItem extends Composite implements HasClickHandlers, HasGrantedResource, HasEnabled {

    private static TooltipMenuIItemUiBinder uiBinder = GWT.create(TooltipMenuIItemUiBinder.class);

    interface TooltipMenuIItemUiBinder extends UiBinder<Widget, ContextualMenuItem> {
    }

    @UiField
    Resources res;

    @UiField
    HTML label;

    private boolean enabled = true;

    private String[] grantedResourceName = null;

    public ContextualMenuItem() {
        initWidget(uiBinder.createAndBindUi(this));
        res.style().ensureInjected();
    }

    public ContextualMenuItem(String text) {
        this();
        setText(text);
    }

    public void setText(String text) {
        label.setText(text);
    }

    public String getText() {
        return label.getText();
    }

    protected void setBorderVisible(boolean visivle) {
        this.addStyleName(res.style().first());
    }

    @Override
    public HandlerRegistration addClickHandler(ClickHandler handler) {
        HandlerRegistration handlerRegistration = new HandlerRegistration() {
            @Override
            public void removeHandler() {
            }
        };

        if (grantedResourceName == null || ResourceAccessHolder.hasAccess(grantedResourceName)) {
            handlerRegistration = addDomHandler(handler, ClickEvent.getType());
        }
        return handlerRegistration;
    }

    @Override
    public void setGrantedResourceName(String[] resourceName) {
        this.grantedResourceName = resourceName;
        setEnabled(ResourceAccessHolder.hasAccess(resourceName));

    }

    public void setGrantedResourceName(String resourceName) {
        setGrantedResourceName(new String[]{resourceName });
    }

    @Override
    public boolean isEnabled() {
        return this.enabled;
    }

    @Override
    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
        if (!enabled) {
            label.addStyleName(res.layout().disabled());
        } else {
            label.removeStyleName(res.layout().disabled());
        }
    }

    @Override
    public String[] getGrantedResourceName() {
        return grantedResourceName;
    }
}
